package com.glory.brepda.activity.mfg;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.EqpStatusEntity;
import com.glory.brepda.entity.TinLayerThicknessCheckEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.entity.waferCutThicknessCheckEntity;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WaferCutThicknessCheckActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.etWaferCutThicknessData)
    FreeUI_ClearEditText checkData;
    @BindView(R.id.teShift)
    TextView teShift;
    @BindView(R.id.teCheckCount)
    TextView teCheckCount;

    Integer count = null;



    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wafer_cut_thickness_check;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("提交");
        setShiftData();

    }



    @Override
    protected void initData() {
        initEdit(etEqpId, this::getCheckRecord);
    }



    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
        }
    }

    private void getCheckRecord(String eqpId) {
        FreeUI_EntityCallBack<BaseResponseBean<waferCutThicknessCheckEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<waferCutThicknessCheckEntity>>
                (new TypeToken<BaseResponseBean<waferCutThicknessCheckEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<waferCutThicknessCheckEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    waferCutThicknessCheckEntity body = response.body().getResponse().getBody();
                    count = body.getRecordCount()+1;
                    if (count!=7){
                        teCheckCount.setText("本班次第" + count + "组数据");
                    }else {
                        teCheckCount.setText("本班次已完成6组数据");
                    }
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<waferCutThicknessCheckEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "waferCutThicknessCheck");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","CHECKRECORD");
        map.addProperty("EQUIPMENTID",eqpId);
        map.addProperty("SHIFT",teShift.getText().toString());
        NetUtils.requestPostNet("IF.WAFERCUTTHICKNESSCHECK","Common", WaferCutThicknessCheckActivity.this,map,callBack);
    }

    private void submit(){
        if(count==7){
            showToast("该设备当班次已完成六组数据，提交失败", false);
            return;
        }
        if ("".equals(getText(etEqpId))){
            showToast("设备号不能为空", false);
            return;
        }
        if ("".equals(getText(checkData))){
            showToast("芯片切痕宽度不能为空", false);
            return;
        }
        String temp = getText(checkData);
        if (!getText(checkData).matches("^[-+]?\\d+(\\.\\d+)?$")){
            showToast("宽度数据输入有误", false);
            return;
        }
        FreeUI_EntityCallBack<BaseResponseBean<waferCutThicknessCheckEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<waferCutThicknessCheckEntity>>
                (new TypeToken<BaseResponseBean<waferCutThicknessCheckEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<waferCutThicknessCheckEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("录入成功！", true);
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<waferCutThicknessCheckEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "waferCutThicknessCheck");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","SAVE");
        map.addProperty("EQUIPMENTID",getText(etEqpId));
        map.addProperty("SHIFT",teShift.getText().toString());
        map.addProperty("CHECKDATA",getText(checkData));
        map.addProperty("COUNT",count);
        NetUtils.requestPostNet("IF.WAFERCUTTHICKNESSCHECK","Common", WaferCutThicknessCheckActivity.this,map,callBack);

    }

    private void setShiftData() {
        Calendar now = Calendar.getInstance();
        String shift = determineShift(now);
        if (now.get(Calendar.HOUR)*60+now.get(Calendar.MINUTE)<7*60+30 && "晚班".equals(shift)){
            now.add(Calendar.DATE, -1);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(now.getTime());
        teShift.setText(dateStr + " " + shift);
    }
    private static String determineShift(Calendar cal) {
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);

        // 将时间转换为分钟
        int timeInMinutes = hour * 60 + minute;

        // 定义班次时间范围（以分钟为单位）
        int earlyShiftStart = 7 * 60 + 30;
        int earlyShiftEnd = 15 * 60 + 30;
        int midShiftStart = 15 * 60 + 30;
        int midShiftEnd = 23 * 60 + 30;
        int lateShiftStart = 23 * 60 + 30;
        int lateShiftEnd = 7 * 60 + 30 + 24 * 60; // 加上一天的分钟数

        if (timeInMinutes >= earlyShiftStart && timeInMinutes < earlyShiftEnd) {
            return "早班";
        } else if (timeInMinutes >= midShiftStart && timeInMinutes < midShiftEnd) {
            return "中班";
        } else if (timeInMinutes >= lateShiftStart || timeInMinutes < lateShiftEnd) {
            return "晚班";
        }
        return "不在班次时间内";
    }


}
